package com.walker.taogong.web;

import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.walker.taogong.bll.TaskBll;
import com.walker.taogong.bll.UserBll;
import com.walker.taogong.datamodel.Task;
import com.walker.taogong.datamodel.TaskStatus;
import com.walker.taogong.datamodel.User;

public class UserMain
{

    /** Logger. */
    private static Logger log = LoggerFactory.getLogger(EditUser.class);

    /************************ BLL 服务引用 *******************************************/

    private UserBll userBll;

    public void setUserBll(UserBll userBll)
    {
        this.userBll = userBll;
    }

    private TaskBll taskBll;

    public void setTaskBll(TaskBll taskBll)
    {
        this.taskBll = taskBll;
    }

    /************************* 启动测试用 ************************************/
    /**
     * 组件启动初期化
     */
    protected void activate(ComponentContext cc)
    {
        log.info("{} is starting!", cc.getProperties().get("name"));

        try
        {
            // 主流程

            // 1. 输入邮箱或者手机号，密码
            // FIXME 在页面进行邮箱和密码的格式检查
            String emailOrMobile = "lisi@test.com";
            String password = "123456";

            User user = userBll.login(emailOrMobile, password);

            if (user == null)
            {
                log.info("输入的用户名或者密码不对");
                log.info("跳转到login登录页面");
            }
            else
            {
                log.info("跳转到UserMain 页");
                showMainPage(user);
                showMyTasks(user);
                showTopTasks();
                showNewestTasks();

            }
        }
        catch (Throwable exc)
        {
            log.error("Terminated with runtime exception", exc);
        }

    }

    private void showMainPage(User user)
    {
        log.info("---------------用户登录后的主页显示-----------------");
        log.info("Link 显示用户名" + user.getUserName());
        log.info("显示头像");
        log.info("显示主页任务分类");
        log.info("显示自己发布的任务");
        log.info("显示最近的所有任务");
        log.info("---------------用户登录后的主页显示-----------------");

    }

    private void showMyTasks(User user)
    {
        log.info("---------------用户自己发布的任务显示-----------------");
        for (Task task : userBll.getMyTasks(user))
        {
            log.info("Link : " + task.getTaskTitle());
            log.info(TaskStatus.valueOf(task.getStatus()) + "日期: "
                    + task.getPostDate());
        }
        log.info("---------------用户自己发布的任务显示-----------------");

    }
    
    
    private void showTopTasks()
    {
        log.info("---------------Top ten 任务显示-----------------");
        for (Task task : taskBll.getTopTasks())
        {
            log.info("Link : " + task.getTaskTitle());
            log.info(TaskStatus.valueOf(task.getStatus()) + "日期: "
                    + task.getPostDate());
        }
        log.info("---------------Top ten 任务显示-----------------");

    }
    
    private void showNewestTasks()
    {
        log.info("---------------Newest ten 任务显示-----------------");
        for (Task task : taskBll.getNewestTasks())
        {
            log.info("Link : " + task.getTaskTitle());
            log.info(TaskStatus.valueOf(task.getStatus()) + "日期: "
                    + task.getPostDate());
        }
        log.info("---------------Newest ten 任务显示-----------------");

    }

}
